package sein.android.hopenote;

import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Note_Mod  extends Activity implements OnClickListener{
	
	public static DBAdapter HnopenHelper;
	public static SQLiteDatabase sdb;
	public static Button bt_save, bt_del, bt_cancel;
	public static EditText et_date, et_time;
	public static TextView tv_title, tv_group, tv_unit;
	public static String str_user, str_group, str_idx, str_item, str_unit, str_cnt, qWhere;
	public static	ArrayList<String> lst_item, lst_value;
	public static	ArrayAdapter<String> adapter;
	public static Cursor cursor;
	public static ListView lv_item;
	private	static ArrayList<ItemData> Item_arr;
	private static Note_Add_Adapter I_adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_mod);
		
		HnopenHelper = new DBAdapter(this);
        sdb = HnopenHelper.getReadableDatabase();
        str_user = HopeNoteDb.CURRENT_USER;
        str_group = HopeNoteDb.CURRENT_GROUP;
        str_item = HopeNoteDb.CURRENT_ITEM;
        str_unit = HopeNoteDb.CURRENT_UNIT;
        str_cnt = HopeNoteDb.CURRENT_CNT;
        str_idx = HopeNoteDb.MODIFY_IDX;
        
        // DB
        if(str_idx != null)
        {

            bt_save = (Button)findViewById(R.id.bt_save);
            bt_del = (Button)findViewById(R.id.bt_del);
            bt_cancel = (Button)findViewById(R.id.bt_cancel);
            tv_title = (TextView)findViewById(R.id.tv_title);
            tv_group = (TextView)findViewById(R.id.tv_group);
            lv_item = (ListView)findViewById(R.id.lv_item);
            et_date = (EditText)findViewById(R.id.et_date);
            et_time = (EditText)findViewById(R.id.et_time);
            
        	tv_group.setText(str_group);

        	qWhere = HopeNoteDb.COL_IDX + " = " + str_idx;
        	cursor = sdb.query(HopeNoteDb.TB_NOTE, new String[] {HopeNoteDb.COL_ITEM,HopeNoteDb.COL_DOCUMENT1,HopeNoteDb.COL_DOCUMENT2, "strftime('%Y-%m-%d'," + HopeNoteDb.COL_DATE + ")", "strftime('%H:%M'," + HopeNoteDb.COL_DATE + ")"}, qWhere, null, null, null, null);
        
        	Item_arr = new ArrayList<ItemData>();
	        ItemData datarow;
	        
	        cursor.moveToFirst();
	        datarow = new ItemData(str_item, str_cnt, str_unit, cursor.getString(1),cursor.getString(2));
	        et_date.setText(cursor.getString(3));
        	et_time.setText(cursor.getString(4));
        	Item_arr.add(datarow);
	        cursor.close();
	        I_adapter = new Note_Add_Adapter(this, R.layout.note_add_row, Item_arr);
	        lv_item.setAdapter(I_adapter);

	        bt_save.setOnClickListener(this);
	        bt_del.setOnClickListener(this);
	        bt_cancel.setOnClickListener(this);
        }        
	}
	
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
		case R.id.bt_save:
			ContentValues row = new ContentValues();
			row.put(HopeNoteDb.COL_DOCUMENT1, ((ItemData)lv_item.getItemAtPosition(0)).document1);
			row.put(HopeNoteDb.COL_DOCUMENT2, ((ItemData)lv_item.getItemAtPosition(0)).document2);
			
			if(sdb.update(HopeNoteDb.TB_NOTE, row, qWhere, null) > 0)
			{
				Toast.makeText(Note_Mod.this, "수정됨", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent("Reset");
				sendBroadcast(intent);
				finish();
			}
			else
			{
				Toast.makeText(Note_Mod.this, "수정 실패", Toast.LENGTH_SHORT).show();
				return;
			}
		case R.id.bt_del:
			if(sdb.delete(HopeNoteDb.TB_NOTE, qWhere, null) > 0)
			{
				Toast.makeText(Note_Mod.this, "삭제됨", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent("Reset");
				sendBroadcast(intent);
				finish();
			}
			else
			{
				Toast.makeText(Note_Mod.this, "삭제 실패", Toast.LENGTH_SHORT).show();
				return;
			}
		case R.id.bt_cancel:
			finish();
		}
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
}
